package cn.touna.jss.common.util;

import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.Map.Entry;

public class CollectionUtil {

    /**
     * Map按值排序
     *
     * @param oriMap
     * @return
     */
    public static Map<String, Integer> sortMapByValue(Map<String, Integer> oriMap) {
        Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
        if (!CommonUtils.isEmpty(oriMap)) {
            List<Entry<String, Integer>> entryList = new ArrayList<Entry<String, Integer>>(oriMap.entrySet());
            Collections.sort(entryList, new Comparator<Entry<String, Integer>>() {
                public int compare(Entry<String, Integer> entry1,
                                   Entry<String, Integer> entry2) {
                    int value1 = 0, value2 = 0;
                    try {
                        value1 = entry1.getValue();
                        value2 = entry2.getValue();
                    } catch (NumberFormatException e) {
                        value1 = 0;
                        value2 = 0;
                    }
                    return value2 - value1;
                }
            });
            Iterator<Entry<String, Integer>> iter = entryList.iterator();
            Entry<String, Integer> tmpEntry = null;
            while (iter.hasNext()) {
                tmpEntry = iter.next();
                sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
            }
        }
        return sortedMap;
    }

    /**
     * @param list      集合
     * @param parameter 根据这个参数排序
     * @param <T>
     */
    public static <T> void sortListOrderDesc(List<T> list, final String parameter) {
        Collections.sort(list, new Comparator<T>() {
            @Override
            public int compare(Object o1, Object o2) {
                try {
                    Object property1 = PropertyUtils.getProperty(o1, parameter);
                    Object property2 = PropertyUtils.getProperty(o2, parameter);
                    if (property1 instanceof Integer) {
                        return (Integer) property2 - (Integer) property1;
                    } else if (property1 instanceof Double) {
                        return (Double) property2 - (Double) property1 > 0.00 ? 1 : -1;
                    } else if (property1 instanceof Long) {
                        return (int) ((Long) property2 - (Long) property1);
                    } else if (property1 instanceof Short) {
                        return (int) ((Short) property2 - (Short) property1);
                    } else if (property1 instanceof String) {
                        if (((String) property1).endsWith("%")) {
                            double i2 = Double.valueOf(((String) property2).replace("%", ""));
                            double i1 = Double.valueOf(((String) property1).replace("%", ""));
                            if (i2 >= i1) {
                                return 1;
                            } else {
                                return -1;
                            }
                        }
                    }
                } catch (IllegalAccessException | InvocationTargetException
                        | NoSuchMethodException e) {
                    e.printStackTrace();
                }
                return 0;
            }

        });
    }

    /**
     * 以空间换时间比较2个集合类
     *
     * @param targetColl
     * @param filterColl 被参照的激活
     * @return
     */
    public static Collection getDiff(Collection targetColl, Collection filterColl) {

        LinkedList resultList = new LinkedList();
        /*Collection collMax=collA;
        Collection collMin=collB;
        if(collA.size()<collB.size()){
            collMax=collB;
            collMin=collA;
        }*/
        Map<Object, Integer> map = new HashMap<>(targetColl.size());
        for (Object object : targetColl) {
            map.put(object, 1);
        }

        for (Object object : filterColl) {
            if (map.get(object) == null) {
                continue;
            }
            map.put(object, 0);
        }

        for (Map.Entry<Object, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                resultList.add(entry.getKey());
            }
        }
        return resultList;
    }


    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            list1.add("test" + i);
            list2.add("test" + i * 2);
        }
        Collection list = CollectionUtil.getDiff(list1, list2);
        System.out.println();
    }
}
